package com.edu.lawdoc.srv;

import com.edu.lawdoc.dao.BaseDao;
import com.edu.lawdoc.dao.Filter;
import com.edu.lawdoc.dao.ModifyingFields;
import com.edu.lawdoc.errors.Errors;
import com.edu.lawdoc.generator.db.output.model.DemoOptions;
import com.edu.lawdoc.generator.field.output.Column;
import com.edu.lawdoc.i18n.I18nDelegate;
import com.edu.lawdoc.pvg.Pvg;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class DemoSrv extends BaseDao<DemoOptions> {

    public DemoSrv() {
        super(DemoOptions.class);
    }

    /**
     *
     * @param fields
     * @return
     */
    public DemoOptions findSpecific(String domain, String optionName, String[] fields) {
        Filter filter = new Filter();
        filter.put(Column.DEMO_OPTIONS.OPTION_NAME, optionName);
        filter.put(Column.DEMO_OPTIONS.DOMAIN, domain);
        return super.findFirst(fields, filter, null);
    }


    /**
     *
     * @param o
     * @return
     */
    public long add(DemoOptions o) {
        o.setCreator(Pvg.getPvgName());
        o.setGmtCreated(new Date());
        o.setModifier(Pvg.getPvgName());

        boolean result = o.save();
        if(!result) {
            throw new IllegalStateException(I18nDelegate.get(Errors.SAVE_ERR));
        }

        return o.getId();
    }

    /**
     * 通过id修改指定值
     * @param id
     * @param version
     * @param value
     */
    public void update(long id, int version, String value) {
        Map<String, Object> paraMap = new HashMap<>();
        paraMap.put(Column.MODIFIER, "hu.xiao");
        paraMap.put(Column.GMT_MODIFIED, new Date());
        paraMap.put(Column.DEMO_OPTIONS.OPTION_VALUE, value);

        ModifyingFields mf = new ModifyingFields(paraMap);

        updateById(id, version, mf);
    }
}
